package com.tools.controller.workflow;

import com.tools.pojo.workflow.dto.StepDefinitionDTO;
import com.tools.pojo.workflow.po.StepDefinition;
import com.tools.service.workflow.StepDefinitionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 步骤定义控制器
 */
@RestController
@RequestMapping("/stepDefinition")
@RequiredArgsConstructor
public class StepDefinitionController {

    private final StepDefinitionService stepDefinitionService;

    /**
     * 创建步骤定义
     */
    @PostMapping("/create")
    public Map<String, Object> createStepDefinition(@RequestBody StepDefinitionDTO dto) {
        try {
            StepDefinition stepDefinition = new StepDefinition();
            stepDefinition.setStepCode(dto.getStepCode());
            stepDefinition.setStepName(dto.getStepName());
            stepDefinition.setStepType(dto.getStepType());
            stepDefinition.setHandlerBean(dto.getHandlerBean());
            stepDefinition.setConfig(dto.getConfig());

            boolean success = stepDefinitionService.createStepDefinition(stepDefinition);

            Map<String, Object> result = new HashMap<>();
            result.put("success", success);
            result.put("message", success ? "步骤定义创建成功" : "步骤定义创建失败");
            result.put("data", stepDefinition);

            return result;

        } catch (Exception e) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "创建步骤定义失败: " + e.getMessage());
            return result;
        }
    }

    /**
     * 获取所有步骤定义
     */
    @GetMapping("/list")
    public Map<String, Object> listStepDefinitions() {
        List<StepDefinition> definitions = stepDefinitionService.getAllStepDefinitions();

        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", definitions);
        result.put("total", definitions.size());

        return result;
    }

}
